package com.excuse.excusebuilder.view.activity;

import android.content.DialogInterface;
import android.graphics.Path;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import com.excuse.excusebuilder.R;
import com.excuse.excusebuilder.adapter.ExcuseActivityAdapter;
import com.excuse.excusebuilder.base.BaseActivity;
import com.excuse.excusebuilder.bean.Option;
import com.excuse.excusebuilder.bean.Result;
import com.excuse.excusebuilder.presenter.ExcuseActivityPresenter;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ExcuseActivity extends BaseActivity implements ExcuseActivityPresenter.IView, SwipeRefreshLayout.OnRefreshListener {
    public static final String PARAM_OPTION = "option";
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar)
    AppBarLayout appBar;
    @BindView(R.id.recycler)
    EasyRecyclerView recycler;
    @BindView(R.id.add_excuse_fab)
    FloatingActionButton addExcuseFab;

    private ExcuseActivityPresenter presenter;
    private ExcuseActivityAdapter adapter;
    private Option option;


    @Override
    protected void initData() {
        adapter = new ExcuseActivityAdapter(this);
        initToolbar(toolbar, "备选借口");
        initRecyclerView(recycler, adapter);
        option = (Option) getIntent().getSerializableExtra(PARAM_OPTION);
        presenter = new ExcuseActivityPresenter(this, this);
        recycler.setRefreshListener(this);


        adapter.setOnItemLongClickListener(new RecyclerArrayAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(int position) {
                final Result result = adapter.getItem(position);

                AlertDialog deleteDialog = new AlertDialog.Builder(ExcuseActivity.this)
                        .setTitle("删除借口")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                presenter.deleteResult(result);
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("否", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .create();
                deleteDialog.show();
                return true;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        onRefresh();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_excuse;
    }


    @Override
    public void setContentList(List<Result> list) {
        Log.e("TAG", "result.lise.size:" + list.size());
        adapter.clear();
        adapter.addAll(list);
    }

    @Override
    public void deleteResult() {
        onRefresh();
    }


    @OnClick(R.id.add_excuse_fab)
    public void onViewClicked() {
        presenter.enterAddResultActivity(option);
    }

    @Override
    public void onRefresh() {
        presenter.getExcuseText(option);
    }

    @Override
    public void showRefreshing(boolean isShow) {
        recycler.setRefreshing(isShow);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
